/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-11-22 16:19:19
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-06-04 18:39:45
 * @FilePath: \cxl-driving-training\src\views\orderManagement\list\index.ts
 */
import { BaseData, BaseMethod } from '@/utils/basc-data'
import { tableConfig } from './config'
import { reactive, watchEffect } from 'vue'
import { FormInstance } from 'element-plus'
import router from '@/router/index'
import { orderList, orderPageInterface } from '@/apis/order'
import { optionsInterface } from '@/utils/dict-data'

export default class OrderListData extends BaseData implements BaseMethod {
    search = {
        orderNo: '',
        orderStatus: '',
        schoolId: '',
        carType: '-1',
        customerSource: '-1',
        time: []
    }
    //驾校
    drivingSchoolOptions = reactive<optionsInterface[]>([])
    constructor() {
        super(tableConfig)
        watchEffect(() => {
            tableConfig.search[2].options = []
            this.uStore.queryDrivingSchoolSimple((res: Map<string, any>) => {
                res.forEach(item => {
                    this.drivingSchoolOptions.push({ label: item.shortName, value: item.id })
                })
                tableConfig.search[2].options?.push(
                    ...(this.drivingSchoolOptions as {
                        label: string
                        value: string
                    }[])
                )
            })
        })

        this.getData()
    }
    getData = (arg?: any) => {
        const params: orderPageInterface = {
            current: this.pageData.pagination.pageNo,
            size: this.pageData.pagination.pageSize
        }

        if (this.search.orderNo) {
            params.orderNo = this.search.orderNo
        }
        if (this.search.schoolId) {
            params.schoolId = this.search.schoolId
        }
        if (this.search.orderStatus !== '' && this.search.orderStatus !== '-1') {
            params.orderStatus = Number(this.search.orderStatus)
        }
        if (this.search.time.length) {
            params.startCreateTime = this.search.time[0]
            params.endCreateTime = this.search.time[1]
        }
        // if (this.search.carType !== '' && this.search.carType !== '-1') {
        //     params.carType = Number(this.search.carType)
        // }
        // if (this.search.customerSource !== '' && this.search.customerSource !== '-1') {
        //     params.customerSource = Number(this.search.customerSource)
        // }
        orderList(params).then(res => {
            if (Number(res.data.code) === 200) {
                this.pageData.table.data.length = 0
                this.pageData.pagination.total = res.data.result.total
                const arr: any[] = res.data.result.records

                arr.forEach((element: any) => {
                    element.status = Number(element.orderStatus) === 0 ? true : false
                    this.pageData.table.data.push(element)
                })
            }
        })
    }
    onSearch = (arg?: any) => {
        console.log(arg)
        this.search = arg
        this.getData()
    }
    onReset = (arg?: any) => {
        this.search = {
            orderNo: '',
            orderStatus: '-1',
            schoolId: '',
            carType: '-1',
            customerSource: '-1',
            time: []
        }
        this.getData()
    }
    onPaginationChange = (arg?: any) => {
        this.pageData.pagination.pageNo = arg
        this.getData()
    }
    onPaginationSize = (arg?: any) => {
        this.pageData.pagination.pageNo = 1
        this.pageData.pagination.pageSize = arg
        this.getData()
    }
    onOperation = (arg?: any) => {
        const { type, data } = arg
        console.log(type, data)
        if (type === 'detailed') {
            router.push({ path: '/order/detailed', query: { id: data.id } })
        }
    }
}
